package com.views;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;

import com.senditcertified.mail.R;


public class ACheckBox extends ImageView implements View.OnClickListener{
	private boolean checked=false;
	private ACheckBox.OnCheckListener lsn;
	public ACheckBox(Context context) {
		super(context);
		init(null);
	}
	public ACheckBox(Context context,AttributeSet tr) {
		super(context,tr);
		init(tr);
	}
	public ACheckBox(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init(attrs);
	}
	
	public void init(AttributeSet attrs){
		this.setImageResource(android.R.color.transparent);
		this.setOnClickListener(this);
		this.setClickable(true);
		if(attrs != null){
			TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.ACheckBox);
			if(a.hasValue(R.styleable.ACheckBox_isChecked)){
				checked=a.getBoolean(R.styleable.ACheckBox_isChecked,true);
			}
			a.recycle();
		}
		setValue();
	}
	public void setChecked(boolean value){
		this.checked=value;
		setValue();
	}
	public boolean isChecked() {
		return checked;
	}
	@Override
	public void onClick(View v) {
		checked=!checked;
		setValue();
		if(lsn!=null) lsn.onCheck(v,checked);
		
	}
	private void setValue(){
		if(checked){
			this.setImageResource(R.drawable.fm_check_mark_sel);
		}else{
			this.setImageResource(R.drawable.fm_check_mark_unsel);
		}
	}
	public void setOnCheckListener(ACheckBox.OnCheckListener lsn){
		this.lsn=lsn;
	}
	public interface OnCheckListener{
		public void onCheck(View v,boolean check);
	}
	
}
